package cn.desk.desktop.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.desk.commons.service.impl.BaseServiceImpl;
import cn.desk.commons.util.QueryCondition;
import cn.desk.desktop.entity.ShortCut;
import cn.desk.desktop.service.IShortCutService;

public class ShortCutServiceImpl<T extends ShortCut> extends BaseServiceImpl<T>
		implements IShortCutService<T> {
	private static final Logger log = LoggerFactory
			.getLogger(ShortCutServiceImpl.class);

//	public PageMode<T> listPage(PageMode<T> page, T t) {
//		log.debug("listPage start");
//		QueryCondition condition = new QueryCondition();
//		if (t.getId() != null && !"".equals(t.getId())) {
//			condition.andEq("id", t.getId());
//		}
//		if (t.getName()!= null && !"".equals(t.getName())) {
//			condition.andLike("name", t.getName(),QueryCondition.MATCH_TYPE_ANY);
//		}
//		condition.desc("type");
//		baseDao.listPage(page, t.getClass(), condition);
//		log.debug("listPage end");
//		return page;
//	}

//	public void add(T t) throws cn.desk.commons.exception.DeskException {
//		log.debug("add start");
//		List<T> list = listByProperty(ShortCut.class, "name", t.getName());
//		if (list.size() != 0) {
//			throw new DeskException("EM0001", t);
//		}
//		String id = getMax(ShortCut.class, "id");
//		if (id == null) {
//			id = "0";
//		}
//		id = "" + (Integer.parseInt(id) + 1);
////		if (t.getParent() != null && "".equals(t.getParent().getId())) {
////			t.setParent(null);
////		}
//		t.setId(id);
//		baseDao.insert(t);
//		log.debug("add end");
//	}

//	public void update(T t) throws DeskException {
//		log.debug("update start");
//		try {
//			if (t.getParent() != null && "".equals(t.getParent().getId())) {
//				t.setParent(null);
//			}
//			super.update(t);
//		} finally {
//			log.debug("update end");
//		}
//
//	}

//	public void delete(T t) throws DeskException {
//		log.debug("delete start");
//		T temp = baseDao.load(t.getClass(), t.getPK());
//		if (temp == null) {
//			throw new DeskException("EM0002", t);
//		}
//		List<T> list = listByProperty(ShortCut.class, "parent", temp);
//		if (list.size() != 0) {
//			throw new DeskException("EMDT0002", temp);
//		}
//		baseDao.delete(temp);
//		log.debug("delete end");
//	}

	@Override
	public List<T> listIndex(T parent) {
		log.debug("listIndex start");
		try {
			QueryCondition condition = new QueryCondition();
			if (parent.getId() == null || "".equals(parent.getId())) {
				condition = condition.andIsNull("parent");
			}else{
				condition.andEq("parent", parent.getId());
			}
			condition.desc("type");
			condition.desc("name");
			return baseDao.list(ShortCut.class, condition);
		} finally {
			log.debug("listIndex end");
		}
	}
}
